package com.demo.search.xmlparser;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.lucene.xmlparser.QueryTemplateManager;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.demo.common.StringUtils;

/**
 * Query模板管理器封装对象
 * <p>封装QueryTemplateManager,并遍历xsl文件夹,加载模板</p>
 * @author ryuu.kk
 *
 */
public abstract class QueryTemplateManagerWrapper extends QueryTemplateManager {

	/**
	 * 查询模板管理器代理
	 */
	private QueryTemplateManager delegate;
	
	/**
	 * 索引路径
	 */
	private String xslPath;
	
	/**
	 * 文件过滤器
	 */
	private FileFilter filter;
	
	/**
	 * 构造函数,创建根
	 * @param xslIs
	 * @throws TransformerConfigurationException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	public QueryTemplateManagerWrapper(InputStream xslIs) throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException {
		delegate = new QueryTemplateManager();
		delegate.addDefaultQueryTemplate(xslIs);
	}
	
	/**
	 * Lucene扩展查询的QueryTemplateManager封装对象构造方法
	 * 遍历xsl所在的文件夹,<br><font color="red">将xsl文件名作为模板名称,xsl内容作为模板存储到模板管理器中</font><br>
	 * 如果xsl文件以关键字"default"起名,则作为默认模板
	 * @param xslPath xsl文件地址
	 * @param filter 过滤器
	 * @throws FileNotFoundException 
	 */
	public QueryTemplateManagerWrapper(String xslPath, FileFilter filter) throws FileNotFoundException {
		
		this.xslPath = xslPath;
		this.filter = filter;
	}
	
	public void addDefaultQueryTemplate(InputStream xslIs)
			throws TransformerConfigurationException,
			ParserConfigurationException, SAXException, IOException {
		delegate.addDefaultQueryTemplate(xslIs);
	}

	public void addQueryTemplate(String name, InputStream xslIs)
			throws TransformerConfigurationException,
			ParserConfigurationException, SAXException, IOException {
		delegate.addQueryTemplate(name, xslIs);
	}

	public String getQueryAsXmlString(Properties formProperties,
			String queryTemplateName) throws SAXException, IOException,
			ParserConfigurationException, TransformerException {
		return delegate.getQueryAsXmlString(formProperties, queryTemplateName);
	}

	public Document getQueryAsDOM(Properties formProperties,
			String queryTemplateName) throws SAXException, IOException,
			ParserConfigurationException, TransformerException {
		return delegate.getQueryAsDOM(formProperties, queryTemplateName);
	}

	public String getQueryAsXmlString(Properties formProperties)
			throws SAXException, IOException, ParserConfigurationException,
			TransformerException {
		return delegate.getQueryAsXmlString(formProperties);
	}

	/**
	 * 获取真实路径
	 * @param path 原始路径path
	 * @return String 查询模板资源真实路径
	 */
	public abstract String getRealPath(String path);
	
	/**
	 * 模板加载器
	 * 装载文件夹内所有符合要求的Lucene查询模板.
	 * 并且以文件名作为key,存放到模板管理器中
	 * @throws Exception 读取异常
	 */
	public void queryTemplateLoader() throws Exception {
		
		xslPath = getRealPath(xslPath);
		// xsl文件地址检查
		if (StringUtils.isEmpty(xslPath)) {
			throw new IllegalArgumentException("xslPath is empty.");
		}
		this.delegate = new QueryTemplateManager();
		//
		File filePath = new File(xslPath);
		File[] xslFileArray = filePath.listFiles(filter);
		for (File xslFile : xslFileArray) {
			InputStream xslStream = new FileInputStream(xslFile);
			try {
				String fileName = xslFile.getName().substring(0, xslFile.getName().lastIndexOf("."));
				// 文件名为"default",则作为默认模板处理
				if ("default".equals(fileName.toLowerCase())) {
					delegate.addDefaultQueryTemplate(xslStream);
				} else {
					delegate.addQueryTemplate(fileName, xslStream);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
